package com.hxf.dao.meal.impl;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.hxf.common.PublicUtil;
import com.hxf.dao.BaseDaoImpl;
import com.hxf.dao.meal.RestaurantDao;
import com.hxf.dto.RestaurantDto;
import com.hxf.dto.ReturnData;
import com.hxf.dto.UserDto;
import com.hxf.exception.DaoLevelException;

@SuppressWarnings("unchecked")
@Repository("restaurantDao")
public class RestaurantDaoImpl extends BaseDaoImpl implements RestaurantDao {

	@Override
	public List<RestaurantDto> findByRestaurant(Integer userId) throws DaoLevelException {
		List<RestaurantDto> list = null;
		String sql = "";
		if(PublicUtil.isNotNull(userId)){
			sql += " and userId = " + userId ;
			
		}
		list = super.findBy("from RestaurantDto where 1=1 " + sql);
		
		return list;
	}
	
	@Override
	public ReturnData<RestaurantDto> returnDataRestaurant(Integer userId) throws DaoLevelException {
		//List<RestaurantDto> list = null;
		if(userId!=null && userId==1){
			return (ReturnData<RestaurantDto>) super.findBy("from RestaurantDto ");
		}else{
			return (ReturnData<RestaurantDto>) super.findBy("from RestaurantDto where userId="+ userId);
		}
		
		
	}
	
	@Override
	public List<RestaurantDto> findByRestaurantId(Integer userId) throws DaoLevelException{
		
		List<RestaurantDto> restDto = null;
		String sql = "";
		if(PublicUtil.isNotNull(userId)){							
			sql = " and userId = "+ userId;
		}
		
		restDto = (List<RestaurantDto>)super.findBy("from  RestaurantDto where 1=1 " + sql);
		
		return restDto;
	}

	/*
	 * 通过 id 查询餐厅
	 */
	@Override
	public List<RestaurantDto> findByRestaurantName(RestaurantDto restantDto) throws DaoLevelException{
		List<RestaurantDto> restDto = null;
	
		if(null != restantDto.getUserId()){							
			restDto = (List<RestaurantDto>)super.findBy("from  RestaurantDto where userId="+ restantDto.getUserId());
		}
		if(null != restantDto.getId()){
			restDto = (List<RestaurantDto>)super.findBy("from  RestaurantDto where id="+ restantDto.getId());
		}
		
		return restDto;
	}

	@Override
	public ReturnData<RestaurantDto> findByRestaurantAllPage(int targetPage,
			int pageSize) throws DaoLevelException { 
		return super.findBy("from RestaurantDto", targetPage, pageSize);
	}

	/*
	 * 通过用户名查询
	 */
	@Override
	public ReturnData<RestaurantDto> findByRestaurantSelectName(
			RestaurantDto restDto, int targetPage, int pageSize)
			throws DaoLevelException {
		String sql = "";
		
		//名称
		if(PublicUtil.isNotNullAndEmpty(restDto.getName())){
			 sql +=" and name like '%"+restDto.getName().trim()+"%'";
		}
		
		//省份
		if(PublicUtil.isNotNullAndEmpty(restDto.getProvince())){
			sql += " and province = '"+restDto.getProvince().trim()+"' ";
		}
		
		//市
		if(PublicUtil.isNotNullAndEmpty(restDto.getCity())){
			sql += " and city = '"+restDto.getCity().trim()+"' ";
		}
			
		//区
		if(PublicUtil.isNotNullAndEmpty(restDto.getDistrict())){
			sql += " and district like '"+restDto.getDistrict().trim()+"'% ";
		}
		
		//街道
		if(PublicUtil.isNotNullAndEmpty(restDto.getStreet())){
			sql += " and street like '"+restDto.getDistrict().trim()+"'% ";
		}
		
		return super.findBy("from RestaurantDto where 1=1 " + sql, targetPage, pageSize);
	}


}
